﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SHJunketingBLL;
using SHJunketingModel;
using Web;
using System.Data;

public partial class Admin_UserManagement_UserChanage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!IsPostBack) 
        {
            ViewState["userPwd"] = "";
            ViewState["userId"] = "";

            GetCbListRole();
            if (Request.QueryString["userId"] != null) 
            {
                int userId = Convert.ToInt32(Request.QueryString["userId"]);
                //查询出用户
                string sql = "select * from users where userid = " + userId;

                DataTable table = DBHelper.GetDataSet(sql);
               
                if (table.Rows.Count > 0)
                {
                    this.txtName.Text = table.Rows[0]["userlogin"].ToString();
                    txtUserName.Text = table.Rows[0]["UserName"].ToString();
                    rdoSex.SelectedValue = table.Rows[0]["UserSex"].ToString();
                    txtDepartment.Text = table.Rows[0]["Department"].ToString();
                    txtCostCenters.Text = table.Rows[0]["CostCenters"].ToString();
                    txtUserIDCard.Text = table.Rows[0]["UserIDCard"].ToString();
                    txtUserAge.Text = table.Rows[0]["UserAge"].ToString();
                    rdoMarriage.SelectedValue = table.Rows[0]["Marriage"].ToString();
                    txtPhoneNumber.Text = table.Rows[0]["HandsetNumber"].ToString();
                    txtTelephone.Text = table.Rows[0]["TelephoneNumber"].ToString();
                    txtAddress.Text = table.Rows[0]["UserAddress"].ToString();
                    txtUserEmai.Text = table.Rows[0]["UserEmail"].ToString();
                    txtArea.Text = table.Rows[0]["Area"].ToString();
                    rdoLang.SelectedValue = table.Rows[0]["language"].ToString();
                    rdoVIP.SelectedValue = table.Rows[0]["whetherVIP"].ToString();
                    rdoUser.SelectedValue = table.Rows[0]["whetherUser"].ToString();
                    txtRemark.Text = table.Rows[0]["userRemark"].ToString();


                    //保存密码和ID
                    ViewState["userPwd"] = table.Rows[0]["UserPwd"];
                    ViewState["userId"] = table.Rows[0]["UserId"];
                }
                    //取出用户的角色
                    List<string> role = C_UserRoleInfoManager.GetUsreLogin(userId);

                    foreach (ListItem li in this.cbListRole.Items)
                    {
                        foreach (string roleName in role)
                        {
                            if (li.Text.Equals(roleName))
                            {
                                li.Selected = true;
                            }
                        }
                    }
            }
        }
    }


    //绑定CheckBoxList
    private void GetCbListRole() 
    {
        IList<C_UserRoleInfo> list = C_UserRoleInfoManager.GetAllC_UserRoleInfos();
        this.cbListRole.DataSource = list;
        this.cbListRole.DataTextField = "RoleName";
        this.cbListRole.DataValueField = "RoleId";
        this.cbListRole.DataBind();
    }


    //修改按钮
    protected void btnEbit_Click(object sender, EventArgs e)
    {
        string sql =
                 "UPDATE users " +
                 "SET " +
                     "UserLogin = '" + txtName.Text.Trim() + "', " +
                     "UserName = '" + txtUserName.Text.Trim() + "'," +
                     "UserSex = '" + rdoSex.SelectedValue + "'," +
                     "Department = '" + txtDepartment.Text.Trim() + "'," +
                     "CostCenters  = '" + txtCostCenters.Text.Trim() + "'," +
                     "UserIDCard = '" + txtUserIDCard.Text.Trim() + "'," +
                     "UserAge = '" + txtUserAge.Text.Trim() + "'," +
                     "Marriage  = '" + rdoMarriage.SelectedValue + "'," +
                     "HandsetNumber = '" + txtPhoneNumber.Text.Trim() + "'," +
                     "TelephoneNumber = '" + txtTelephone.Text.Trim() + "'," +
                     "UserAddress = '" + txtAddress.Text.Trim() + "'," +
                     "UserEmail = '" + txtUserEmai.Text.Trim() + "'," +
                     "Area  = '" + txtArea.Text.Trim() + "'," +
                      "whetherVIP  = '" + rdoVIP.SelectedValue + "'," +
                      "whetherUser  = '" + rdoUser.SelectedValue + "'," +
                       "userRemark  = '" + txtRemark.Text.Trim() + "'," +
                     "language  = '" + rdoLang.SelectedValue + "'" +

                 "WHERE UserID = '" + ViewState["userId"].ToString() + "'";

        DBHelper.ExecuteCommand(sql);

        if (ViewState["userId"] != null)
        {
            C_UserRoleManager.DeleteC_userRoles(Convert.ToString(ViewState["userId"]));
        }
        foreach (ListItem li in this.cbListRole.Items)
        {
            if (li.Selected == true) 
            {
                C_UserRole userRole = new C_UserRole();
                userRole.UserId = Convert.ToInt32(ViewState["userId"]);
                userRole.RoleId = Convert.ToInt32(li.Value);
                C_UserRoleManager.AddC_UserRole(userRole);
            }
        }
        Web.CommFun.Show_Msg("修改成功！", "ProfileUsers.aspx");
    }
}
